package com.hserver.docker.controller;

import com.hserver.docker.config.ConstConfig;
import com.hserver.docker.service.DeployService;
import com.hserver.docker.service.DockerService;
import top.hserver.core.interfaces.HttpResponse;
import top.hserver.core.ioc.annotation.Autowired;
import top.hserver.core.ioc.annotation.Controller;
import top.hserver.core.ioc.annotation.GET;
import top.hserver.core.ioc.annotation.POST;
import top.hserver.core.server.util.JsonResult;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hxm
 */
@Controller
public class IndexController {

    @Autowired
    private DeployService deployService;

    @Autowired
    private DockerService dockerService;


    @GET("/")
    public void index(HttpResponse response) {
        Map<String, Object> data = new HashMap<>();
        data.put("deployList", deployService.getList());
        data.put("dockerUrl", ConstConfig.DOCKER_URL);
        response.sendTemplate("index.ftl", data);
    }


    @POST("/setDockerUrl")
    public JsonResult setDockerUrl(String dockerUrl) {
        if (dockerUrl != null && dockerUrl.trim().length() > 0) {
            Boolean aBoolean = dockerService.checkDockerUrl(dockerUrl);
            if (aBoolean) {
                ConstConfig.DOCKER_URL=dockerUrl;
                return JsonResult.error("设置完成");
            } else {
                return JsonResult.error("哎呀，地址有问题哦！");
            }
        }
        return JsonResult.error("参数不能为空");
    }

}
